package com.example.tys.gif;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private ImageView iv;
    private GifProcess gifProcess;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = (ImageView) findViewById(R.id.iv);

    }


    public void 播放gif(View view) {
        gifProcess = new GifProcess("/mnt/shared/Image/demo.gif");
        int width = gifProcess.getWidth();
        int height = gifProcess.getHeight();
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);


        int delay = gifProcess.updateFrame(bitmap);
        iv.setImageBitmap(bitmap);
        handler.sendEmptyMessageDelayed(1, delay);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int mNextFrame = gifProcess.updateFrame(bitmap);
            iv.setImageBitmap(bitmap);
            handler.sendEmptyMessageDelayed(1, mNextFrame);
        }
    };
}
